package com.example.tetstmybatis.service;

import com.example.tetstmybatis.domain.User2;
import com.example.tetstmybatis.domain.User2Example;
import com.example.tetstmybatis.domain.User_sb;
import com.example.tetstmybatis.domain.User_sbExample;
import com.example.tetstmybatis.mapper.User2Mapper;
import com.example.tetstmybatis.mapper.User_sbMapper;
import com.example.tetstmybatis.req.RepUser_sb;
import com.example.tetstmybatis.req.User2QueryReq;
import com.example.tetstmybatis.req.User2RegisterReq;
import com.example.tetstmybatis.resp.CommonResp;
import com.example.tetstmybatis.resp.User2QueryResp;
import com.example.tetstmybatis.utils.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class User_sbService {
    @Resource
    private User_sbMapper user_sbMapper;

    public CommonResp rzall(RepUser_sb req) {
        User_sbExample user_sbExample = new User_sbExample();
        User_sbExample.Criteria criteria = user_sbExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getRealName())) {
            criteria.andRealNameEqualTo(req.getRealName());
        }
        List<User_sb> user2s = user_sbMapper.selectByExample(user_sbExample);//
        List<User_sb> QueryResps = CopyUtil.copyList(user2s, User_sb.class);
        CommonResp objectCommonResp = new CommonResp();
        objectCommonResp.setContent(QueryResps);
        return objectCommonResp;
    }

    public boolean add(RepUser_sb req) {
        User_sb user_sb = CopyUtil.copy(req, User_sb.class);
        int insert = user_sbMapper.insert(user_sb);
        if (insert == 0) {
            return false;
        } else {
            return true;
        }
    }

    public User_sb find(String WxName) {
        User_sbExample user_sbExample = new User_sbExample();
        User_sbExample.Criteria criteria = user_sbExample.createCriteria();
        criteria.andWxNameEqualTo(WxName);
        List<User_sb> u_list = user_sbMapper.selectByExample(user_sbExample);
        if (CollectionUtils.isEmpty(u_list)) {
            return null;
        } else {
            return u_list.get(0);
        }
    }

    public boolean update(User_sb rep) {
        User_sbExample user_sbExample = new User_sbExample();
        User_sbExample.Criteria criteria = user_sbExample.createCriteria();
        criteria.andWxNameEqualTo(rep.getWxName());
        rep.setZt("签到成功");
        User_sb user_sb = CopyUtil.copy(rep, User_sb.class);
        int insert = user_sbMapper.updateByExample(user_sb, user_sbExample);
        if (insert == 0) {
            return false;
        } else {
            return true;
        }
    }

    public boolean publish() {
        User_sbExample user_sbExample = new User_sbExample();
        User_sbExample.Criteria criteria = user_sbExample.createCriteria();
        List<User_sb> user2s = user_sbMapper.selectByExample(user_sbExample);
        for(int i=0;i<user2s.size();i++)
        {
            User_sb user_sb=user2s.get(i);
            user_sb.setZt("未签到");
            User_sbExample user_sbExample1 = new User_sbExample();
            User_sbExample.Criteria criteria1 = user_sbExample1.createCriteria();
            criteria1.andWxNameEqualTo(user_sb.getWxName());
            int insert = user_sbMapper.updateByExample(user_sb, user_sbExample1);
            if (insert == 0) {
                return false;
            }
        }
        return true;
    }
}
